package com.gisuni.sddc.platformManager.controller;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.ServerInfo;
import com.gisuni.sddc.platformManager.service.ServerInfoService;
import javax.servlet.http.HttpServletRequest;


@IocBean(singleton = false)
@At("/client/backgroundManagement/serverController")
public class ServerInfoController   {

	private Log log = Logs.getLog(ServerInfoController.class);
 
	
	@Inject
    private ServerInfoService serverInfoService;

 
	@At("/dataGrid")
	@GET
	@POST
	@Ok("raw:json") 
    public Object dataGrid(@Param("name") String name,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;
			res = serverInfoService.queryBaseServerInfoAll(page,rows,name);
			r = Response.toJSONObject(res);

		} catch (Exception e) {
			log.error(e);
		}
		return r;
    }
 
 
	@At("/add")
	@GET
	@POST
	@Ok("json")
    public Object add(HttpServletRequest request) {
       
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =   serverInfoService.addServerInfo(request);//返回格式:{}
		} catch (Exception e) {
			log.error(e);
			res.setMsg("操作失败");
		}
		return res;
    }
    
	
	@At("/edit")
	@GET
	@POST
	@Ok("json")
    public Object edit(HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = serverInfoService.updateServerInfo(request); 
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg("操作失败");
		}
		return res;
    }
	

	@At("/delete")
	@GET
	@POST
	@Ok("json")
    public Object delete(String id) {
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =  serverInfoService.deleteServerInfoById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg("操作失败");
		}
		return res;
    }

    
	@At("/editPage")
	@GET
	@POST
	@Ok("jsp:/client/backgroundManagement/views/admin/serverInfoEdit.jsp")
    public String editPage( HttpServletRequest request, String id) {
        ServerInfo serverInfo = serverInfoService.findRoleById(id);
        request.setAttribute("entity", serverInfo);
        return null;
    }
 
 
}
